﻿Imports System.IO
Public Class frm_RemedyData
    Dim ds As New DataSet("datasample")
    Public Structure OldDataStructure
        Dim Data As ArrayList
    End Structure
    Public Structure SNumber
        Dim StartNo As String
        Dim EndNo As String
    End Structure
    Public Structure SearchData
        Dim SearchNumber As List(Of SNumber)
        Dim CheckLength As Integer
        Dim Suffix As String
        Dim Prefix As String
    End Structure
    Public Enum EventAction
        None = 0
        READOLDDATA = 1
        READFINDNUMBER = 2
        WRITEFINDRESULT = 3
    End Enum
    Private Structure ParamText
        Dim Value As String
        Dim Index As Integer
    End Structure
    Public CurrentValue As Long
    Dim ReadData As New List(Of OldDataStructure)
    Private Delegate Sub InvokeChangeProgess(ByVal Value As Long)
    Private Delegate Sub InvokeEnd(ByVal IsError As Boolean, ByVal ErrText As String)
    Private Delegate Sub InvokeEndRead()
    Private Delegate Sub InvokeShowSample(ByVal Parame As ParamText)
    Private Const MaxHeight As Integer = 406
    Private Const MinHeight As Integer = 380
    Dim th As Threading.Thread, fh As Threading.Thread
    Private EventState As EventAction = EventAction.None, ProgressIndex As Integer, ProgressText As String = "...................."
    Dim OldDataSelectedIndex As Integer = 0
    Dim FiledSize As Long = 0
    Private Sub Btn_ReadData_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub frm_RemedyData_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        If EventState <> EventAction.None Then If MessageBox.Show(Me, "有操作未完成是否继续退出？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then  : Return
        e.Cancel = False
    End Sub

    Private Sub frm_RemedyData_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ChangeButtonFromFrm(Me.GroupBox1)
        ChangeButtonFromFrm(Me.GroupBox2)
        ChangeButtonColor(Btn_OrderNumber)
        With LV_FindData
            .Columns(0).Width = .Width / 2 - 4
            .Columns(1).Width = .Width / 2 - 2
        End With
        With LV_DataSample
            .Columns(0).Width = 50
            .Columns(1).Width = .Width - .Columns(0).Width - 4
        End With
        Me.Height = MinHeight
        ' NeroBar1.GlowMode = NeroBar.NeroBarGlowModes.None
        ViewDataMenu.Renderer = MenuSkinRender
    End Sub

    Private Sub TB_Lines_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_Lines.KeyPress
        If Asc(e.KeyChar) = 8 Then Return
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True
    End Sub

    Private Sub TB_DataPath_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_DataPath.KeyPress
        e.Handled = True
    End Sub

    Private Sub TB_FindDataPath_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_FindDataPath.KeyPress
        e.Handled = True
    End Sub

    Private Sub TB_CheckLenght_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_CheckLenght.KeyPress
        If Asc(e.KeyChar) = 8 Then Return
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True
    End Sub

    Private Sub TB_CheckLenght_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_CheckLenght.LostFocus
        If Val(TB_CheckLenght.Text) < 0 Then MsgBox("校验码长度不能小于0！", MsgBoxStyle.Critical, "错误") : TB_CheckLenght.Text = 0
    End Sub

    Private Sub TB_Lines_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_Lines.LostFocus
        If Val(TB_Lines.Text) < 1 Then MsgBox("校验码长度不能小于0！", MsgBoxStyle.Critical, "错误") : TB_Lines.Text = 1
    End Sub

    Private Sub TB_Lines_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB_Lines.TextChanged
        If ReadData.Count <= 0 Then
            With LV_DataSample
                .Items.Clear()
                For i = 0 To Val(TB_Lines.Text) - 1
                    .Items.Add("Var" & i + 1)
                Next
                Dim FileName As String = TB_DataPath.Text
                Dim sCount As Integer = 0
                Dim TmpValue As String = Nothing
                If File.Exists(FileName) Then
                    Dim fs As New FileStream(FileName, FileMode.Open, FileAccess.Read)
                    Dim fw As New StreamReader(fs, System.Text.Encoding.Default)
                    Do Until sCount >= Val(TB_Lines.Text)
                        Dim ReadTmp As String = fw.ReadLine
                        Dim tmpItem As ListViewItem.ListViewSubItem
                        Try
                            tmpItem = LV_DataSample.Items(sCount).SubItems(1)
                        Catch
                            tmpItem = Nothing
                        End Try
                        If tmpItem IsNot Nothing Then
                            tmpItem.Text = ReadTmp
                        Else
                            LV_DataSample.Items(sCount).SubItems.Add(ReadTmp)
                        End If
                        If String.IsNullOrEmpty(TmpValue) Then
                            TmpValue = ReadTmp
                        Else
                            TmpValue = TmpValue & vbCrLf & ReadTmp
                        End If
                        sCount += 1
                    Loop
                    fw.Close()
                    fs.Close()
                    '  If Not String.IsNullOrEmpty(TmpValue) Then TmpValue += vbCrLf : FiledSize = System.Text.Encoding.Default.GetByteCount(TmpValue)
                End If
            End With
        End If
    End Sub

    Private Sub Btn_OpenOldData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_OpenOldData.Click
        With FileDialog
            .Title = "打开源数据文件"
            .Filter = "文本文件(*.txt)|*.txt"
            .FileName = "1.txt"
            .CheckFileExists = True
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                TB_DataPath.Text = .FileName
                TB_DataPath.SelectionStart = .FileName.Length
                Dim tmp As String = ShowInput(Me, "每组行数", 1)
                If Val(tmp) > 1 Then
                    Me.TB_Lines.Text = tmp
                Else
                    TB_Lines_TextChanged(sender, e)
                End If
                ReadData.Clear()
                Btn_StartFindData.Enabled = False

            End If
        End With
    End Sub

    Private Sub Btn_ReadOldData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ReadOldData.Click
        If File.Exists(TB_DataPath.Text) = False Then MessageBox.Show(Me, "数据文件不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error) : Return
        EventState = EventAction.READOLDDATA
        Timer1.Interval = 1000
        ProgressIndex = 0
        Timer1.Enabled = True
        Btn_OpenOldData.Enabled = False
        Btn_ReadOldData.Enabled = False
        OldDataSelectedIndex = 0
        th = New Threading.Thread(New Threading.ThreadStart(AddressOf ReadOldData))
        th.Start()
    End Sub
    Private Sub ReadOldData()
        If File.Exists(TB_DataPath.Text) Then
            Dim sIndex As Integer = 0, sCount As Integer, hasError As Boolean = False, ErrTxt As String = ""
            sCount = Val(TB_Lines.Text)
            Dim tmpData As New OldDataStructure, IsRead As Boolean = False
            ReadData = New List(Of OldDataStructure)
            Dim fs As New FileStream(TB_DataPath.Text, FileMode.Open, FileAccess.Read)
            If FiledSize <= 0 Then
                Dim fw As New StreamReader(fs, System.Text.Encoding.Default)
                Try
                    Do Until fw.EndOfStream
                        Dim tmp As String = fw.ReadLine
                        sIndex = sIndex + 1
                        If sCount > 1 Then
                            If sIndex > 1 And sIndex < sCount Then GoTo AddOnlyData
                            If sCount = 1 And sIndex = 1 Then GoTo AddData
                            Select Case sIndex
                                Case 1
                                    tmpData = New OldDataStructure
                                    tmpData.Data = New ArrayList
                                    tmpData.Data.Add(tmp)
                                Case sCount
AddData:
                                    If tmpData.Data Is Nothing Then tmpData = New OldDataStructure : tmpData.Data = New ArrayList
                                    tmpData.Data.Add(tmp)
                                    ReadData.Add(tmpData)
                                    If IsRead = False Then
                                        For i = 0 To tmpData.Data.Count - 1
                                            Dim TmpP As New ParamText
                                            TmpP.Value = tmpData.Data(i)
                                            TmpP.Index = i
                                            Me.Invoke(New InvokeShowSample(AddressOf ShowSampleData), TmpP)
                                        Next
                                        IsRead = True
                                    End If
                                    sIndex = 0
                                Case sIndex > 1 And sIndex < sCount
AddOnlyData:
                                    tmpData.Data.Add(tmp)
                                    ' Me.Text = tmp
                            End Select
                        Else
                            tmpData = New OldDataStructure
                            tmpData.Data = New ArrayList
                            tmpData.Data.Add(tmp)
                            ReadData.Add(tmpData)
                            If IsRead = False Then
                                Dim TmpP As New ParamText
                                TmpP.Value = tmp
                                TmpP.Index = 0
                                Me.Invoke(New InvokeShowSample(AddressOf ShowSampleData), TmpP)
                                IsRead = True
                            End If
                        End If
                        ' My.Application.DoEvents()
                    Loop

                    '    MsgBox(ReadData(0).Data.Item(2))
                Catch ex As Exception
                    hasError = True
                    ErrTxt = ex.Message
                End Try
                fw.Close()
                fs.Close()
            Else
                Do Until fs.Position >= fs.Length
                    Dim tmpValue As String
                    Dim tmpByte As Byte()
                    Dim a() As String
                    ReDim tmpByte(FiledSize - 1)
                    fs.Read(tmpByte, 0, FiledSize)
                    tmpValue = System.Text.Encoding.Default.GetString(tmpByte)
                    tmpData = New OldDataStructure
                    tmpData.Data = New ArrayList
                    a = tmpValue.Split(vbCrLf)
                    For i = 0 To a.Length - 1
                        tmpData.Data.Add(a(i))
                    Next
                    ReadData.Add(tmpData)
                Loop
            End If
            If Not hasError Then
                MessageBox.Show("读取文件结束，共有：" & ReadData.Count & "项数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show(ErrTxt, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            Me.Invoke(New InvokeEndRead(AddressOf EndReadOldData))
            Try
                fh.Abort()
            Catch ex As Exception
            End Try
        End If
    End Sub
    Private Sub ShowSampleData(ByVal parame As ParamText)
        Dim sIndex As Integer = parame.Index
        Dim litem As ListViewItem = LV_DataSample.Items(parame.Index)
        Dim subItem As ListViewItem.ListViewSubItem
        Try
            subItem = litem.SubItems(1)
            subItem.Text = parame.Value
        Catch
            litem.SubItems.Add(parame.Value)
            Return
        End Try
    End Sub
    Private Sub EndReadOldData()
        Timer1.Enabled = False
        Me.Height = MinHeight
        Me.Text = "提取号码"
        EventState = EventAction.None
        Btn_OpenOldData.Enabled = True
        Btn_ReadOldData.Enabled = True
        Btn_StartFindData.Enabled = True
        If ReadData.Count > 0 Then Btn_OrderNumber.Enabled = True
    End Sub
    Private Sub Btn_ReadFindData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ReadFindData.Click
        If File.Exists(TB_FindDataPath.Text) Then
            LV_FindData.Items.Clear()
            Dim fs As New FileStream(TB_FindDataPath.Text, FileMode.Open, FileAccess.Read)
            Dim fw As New StreamReader(fs, System.Text.Encoding.Default)
            Do Until fw.EndOfStream
                Dim tmp As String = fw.ReadLine
                If tmp <> "" Then
                    Dim a As Integer, R() As String
                    a = tmp.IndexOf(",")
                    If a > 0 Then
                        R = Strings.Split(tmp, ",")
                        If IsNumeric(R(0)) And IsNumeric(R(1)) Then
                            With LV_FindData
                                Dim TmpItem As New ListViewItem
                                TmpItem.Text = R(0)
                                TmpItem.SubItems.Add(R(1))
                                .Items.Add(TmpItem)
                            End With
                        End If
                    Else
                        If IsNumeric(tmp) Then
                            Dim tmpItem As New ListViewItem
                            tmpItem.Text = tmp
                            tmpItem.SubItems.Add(tmp)
                            LV_FindData.Items.Add(tmpItem)
                        End If
                    End If
                End If
            Loop
            fw.Close()
            fs.Close()
        End If
    End Sub

    Private Sub Btn_OpenFindDataPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_OpenFindDataPath.Click
        With FileDialog
            .Title = "打开要取号码信息文本"
            .FileName = "1.txt"
            .CheckFileExists = True
            .Filter = "文本文件(*.txt)|*.txt"
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                TB_FindDataPath.Text = .FileName
                TB_FindDataPath.SelectionStart = .FileName.Length
            End If
        End With
    End Sub

    Private Sub Btn_DelData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_DelData.Click
        For Each item As ListViewItem In LV_FindData.Items
            If item.Selected Then LV_FindData.Items.Remove(item)
        Next
    End Sub

    Private Sub Btn_AddData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_AddData.Click
        If String.IsNullOrEmpty(TB_StartNo.Text) And String.IsNullOrEmpty(TB_EndNo.Text) Then
            MsgBox("起始号码和终止号码不能为空！", MsgBoxStyle.Information, "错误")
            Return
        End If
        If Not (IsNumeric(TB_StartNo.Text)) Or Not (IsNumeric(TB_EndNo.Text)) Then
            MsgBox("起始号码和终止号码必须为数值型数据！", MsgBoxStyle.Critical, "错误")
            Return
        End If
        If Val(TB_StartNo.Text) > Val(TB_EndNo.Text) Then MessageBox.Show(Me, "起始号码不能大于终止号码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) : Return
        Dim tmpItem As New ListViewItem
        tmpItem.Text = TB_StartNo.Text
        tmpItem.SubItems.Add(TB_EndNo.Text)
        LV_FindData.Items.Add(tmpItem)
    End Sub
    Private Function getOnlyNumber(ByVal Value As String, Optional ByVal prefix As String = "", Optional ByVal suffix As String = "", Optional ByVal CheckLength As Integer = 0) As String
        Dim Result As String = Value
        If Not (String.IsNullOrEmpty(prefix)) Then
            Result = Result.Substring(prefix.Length)
        End If
        If Not (String.IsNullOrEmpty(suffix)) Then
            Result = Result.Substring(0, Result.Length - suffix.Length)
        End If
        If CheckLength >= 1 Then
            Result = Result.Substring(0, Result.Length - CheckLength)
        End If
        Return Result
    End Function

    Private Sub Btn_StartFindData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_StartFindData.Click
        Me.Height = MaxHeight
        If ReadData.Count <= 0 Then MessageBox.Show(Me, "未读取源数据，请先读取源文本数据再进行查找操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) : Return
        If LV_FindData.Items.Count <= 0 Then MessageBox.Show(Me, "未设置要查找的号码段，不能进行查找操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) : Return
        Dim SearchInfo As New SearchData
        With SearchInfo
            .CheckLength = Val(TB_CheckLenght.Text)
            .Prefix = TB_Prefix.Text
            .Suffix = TB_Suffix.Text
            Dim Data As New List(Of SNumber)
            For i = 0 To LV_FindData.Items.Count - 1
                Dim TmpItem As New SNumber
                TmpItem.StartNo = LV_FindData.Items(i).Text
                TmpItem.EndNo = LV_FindData.Items(i).SubItems(1).Text
                Data.Add(TmpItem)
            Next
            .SearchNumber = Data
        End With
        SetObjectEnabled(GroupBox1, False)
        SetObjectEnabled(GroupBox2, False)
        SetObjectEnabled(GroupBox3, False)
        NeroBar1.Maximum = ReadData.Count
        EventState = EventAction.WRITEFINDRESULT
        Timer1.Interval = 1
        Timer1.Enabled = True
        Dim SaveFile As String = TB_SaveFileName.Text
        If File.Exists(SaveFile) Then TB_SaveFileName.Text = getAddFileName(SaveFile) : TB_SaveFileName.SelectionStart = TB_SaveFileName.Text.Length
        th = New Threading.Thread(AddressOf StartFindData)
        th.Start(SearchInfo)
    End Sub
    Private Sub StartFindData(ByVal Param As SearchData)
        Dim CaculateStr As String
        Dim TmpValue As Long, LSHP As Integer = Val(TB_Position.Text) - 1
        Dim fs As New FileStream(TB_SaveFileName.Text, FileMode.Create, FileAccess.Write)
        Dim fw As New StreamWriter(fs, System.Text.Encoding.Default)
        Try
            For i = 0 To ReadData.Count - 1
                Dim ExistB As Boolean = False
                CaculateStr = ReadData.Item(i).Data.Item(LSHP).ToString
                TmpValue = Val(getOnlyNumber(CaculateStr, Param.Prefix, Param.Suffix, Param.CheckLength))
                For j = 0 To Param.SearchNumber.Count - 1
                    If TmpValue >= Val(Param.SearchNumber.Item(j).StartNo) And TmpValue <= Val(Param.SearchNumber(j).EndNo) Then
                        ExistB = True
                        Exit For
                    End If
                Next
                If ExistB Then
                    For z = 0 To ReadData.Item(i).Data.Count - 1
                        fw.WriteLine(ReadData.Item(i).Data.Item(z).ToString)
                    Next
                End If
                ' Me.Invoke(New InvokeChangeProgess(AddressOf ChangeProgress), i + 1)
                CurrentValue = i + 1
            Next
            fw.Close()
            fs.Close()
            Try
                th.Abort()
                Me.Invoke(New InvokeEnd(AddressOf EndFind), False, "")
            Catch
            End Try
        Catch ex As Exception
            fw.Close()
            fs.Close()
            If ex.Message <> "正在中止线程。" Then
                Me.Invoke(New InvokeEnd(AddressOf EndFind), True, ex.Message)
            Else
                Me.Invoke(New InvokeEnd(AddressOf EndFind), False, "")
            End If
            Try
                th.Abort()
            Catch
            End Try
        End Try
    End Sub
    Private Sub ChangeProgress(ByVal Value As Long)
        NeroBar1.Value = Value
    End Sub
    Private Sub EndFind(ByVal IsErr As Boolean, ByVal ErrText As String)
        If IsErr = False Then
            MessageBox.Show(Me, "数据操作结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(Me, ErrText, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        SetObjectEnabled(GroupBox1, True)
        SetObjectEnabled(GroupBox2, True)
        SetObjectEnabled(GroupBox3, True)
        Me.Height = MinHeight
        Timer1.Enabled = False
        NeroBar1.Value = 0
        EventState = EventAction.None
    End Sub
    Private Sub TB_DataPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB_DataPath.TextChanged
        Me.TB_SaveFileName.Text = getAddFileName(TB_DataPath.Text)
        TB_SaveFileName.SelectionStart = TB_SaveFileName.Text.Length
    End Sub

    Private Sub TB_StartNo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_StartNo.KeyPress
        If Asc(e.KeyChar) = 8 Then Return
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True
    End Sub

    Private Sub TB_EndNo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_EndNo.KeyPress
        If Asc(e.KeyChar) = 8 Then Return
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then e.Handled = True
    End Sub
    Private Sub SetObjectEnabled(ByVal SearchParent As Control, ByVal Value As Boolean)
        If SearchParent.Controls.Count > 0 Then
            For Each item As Control In SearchParent.Controls
                item.Enabled = Value
            Next
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If EventState = EventAction.WRITEFINDRESULT Then
            NeroBar1.Value = CurrentValue
        ElseIf EventState = EventAction.READOLDDATA Then
            ProgressIndex += 1
            If ProgressIndex > 10 Then ProgressIndex = 1
            Me.Text = "读取数据" & ProgressText.Substring(0, ProgressIndex)
        End If
    End Sub

    Private Sub frm_RemedyData_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

    End Sub

    Private Sub LV_DataSample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LV_DataSample.Click

    End Sub

    Private Sub LV_DataSample_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LV_DataSample.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If ReadData.Count <= 0 Then
                For Each item As ToolStripItem In ViewDataMenu.Items
                    item.Enabled = False
                Next
            Else
                For Each item As ToolStripItem In ViewDataMenu.Items
                    item.Enabled = True
                Next
                If OldDataSelectedIndex = 0 Then Menu_Back.Enabled = False : Menu_First.Enabled = False
                If OldDataSelectedIndex = ReadData.Count - 1 Then Menu_Next.Enabled = False : Menu_Last.Enabled = False
            End If
        End If
    End Sub
    Private Sub ShowText(ByVal Index As Long)
        If Index < 0 Or Index >= ReadData.Count Then Return
        Dim tmpData As OldDataStructure
        tmpData = ReadData(Index)
        For i = 0 To tmpData.Data.Count - 1
            Dim tmpItem As ListViewItem.ListViewSubItem
            Try
                tmpItem = LV_DataSample.Items(i).SubItems(1)
                tmpItem.Text = tmpData.Data(i)
            Catch ex As Exception
                Try
                    tmpItem = LV_DataSample.Items(i).SubItems.Add(tmpData.Data(i))
                Catch
                End Try
            End Try
        Next
    End Sub

    Private Sub Menu_Back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Back.Click
        If OldDataSelectedIndex - 1 >= 0 Then
            OldDataSelectedIndex = OldDataSelectedIndex - 1
            ShowText(OldDataSelectedIndex)
        End If
    End Sub

    Private Sub Menu_Next_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Next.Click
        If OldDataSelectedIndex + 1 < ReadData.Count Then
            OldDataSelectedIndex = OldDataSelectedIndex + 1
            ShowText(OldDataSelectedIndex)
        End If
    End Sub

    Private Sub Menu_First_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_First.Click
        OldDataSelectedIndex = 0
        ShowText(OldDataSelectedIndex)
    End Sub

    Private Sub Menu_Last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Last.Click
        OldDataSelectedIndex = ReadData.Count - 1
        ShowText(OldDataSelectedIndex)
    End Sub

    Private Sub Menu_Goto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Goto.Click
        Dim tmp As String = ShowInput(Me)
        Dim a As Long = Val(tmp) - 1
        If a <> OldDataSelectedIndex And a >= 0 And a < ReadData.Count Then
            OldDataSelectedIndex = a
            ShowText(OldDataSelectedIndex)
        End If
    End Sub

    Private Sub TB_StartNo_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB_StartNo.Enter
        TB_StartNo.SelectionStart = 0
        TB_StartNo.SelectionLength = TB_StartNo.Text.Length
    End Sub

    Private Sub TB_EndNo_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB_EndNo.Enter
        TB_EndNo.SelectionStart = 0
        TB_EndNo.SelectionLength = TB_EndNo.Text.Length
    End Sub

    Private Sub TB_Position_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_Position.KeyPress
        If Asc(e.KeyChar) = 8 Then Return
        If Asc(e.KeyChar) <= 48 Or Asc(e.KeyChar) > Asc(TB_Lines.Text) Then e.Handled = True
        Dim tmp As String = TB_Position.Text & e.KeyChar
        If Val(tmp) > Val(TB_Lines.Text) Then e.Handled = True
    End Sub

    Private Sub TB_Position_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_Position.LostFocus
        If String.IsNullOrEmpty(TB_Position.Text) Then TB_Position.Text = "1" : Return

    End Sub

    Private Sub TB_Position_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_Position.TextChanged

    End Sub

    Private Sub Btn_OrderNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_OrderNumber.Click
        If ReadData.Count <= 0 Then MessageBox.Show(Me, "未读入任何数据，请先读取源数据！", "提示") : Btn_OrderNumber.Enabled = False
        If ReadData.Count Mod 2 <> 0 Then MessageBox.Show(Me, "数据不是偶数项，不能进行转换！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error) : Return
        If RadioButton1.Checked Then
            NeroBar1.Maximum = ReadData.Count
        Else
            NeroBar1.Maximum = ReadData.Count / 2
        End If
        Timer1.Interval = 1
        Timer1.Enabled = True
        Me.Height = MaxHeight
        SetObjectEnabled(GroupBox1, False)
        SetObjectEnabled(GroupBox2, False)
        SetObjectEnabled(GroupBox3, False)
        EventState = EventAction.WRITEFINDRESULT
        Dim gth As Threading.Thread
        gth = New Threading.Thread(AddressOf StartSort)
        gth.Start()
    End Sub
    Private Sub StartSort()
        Try
            Dim ZX As Boolean = True, LSHP As Integer = Val(TB_Position.Text) - 1
            Dim Result1 As New List(Of OldDataStructure), Result2 As New List(Of OldDataStructure)
            Dim Result As New List(Of OldDataStructure)
            ZX = RadioButton1.Checked
            If ZX Then
                For i = 0 To ReadData.Count - 1
                    If i Mod 2 = 0 Then
                        Result1.Add(ReadData.Item(i))
                    Else
                        Result2.Add(ReadData.Item(i))
                    End If
                    CurrentValue += 1
                Next
                Result.AddRange(Result1)
                Result.AddRange(Result2)
                ReadData = Result
            Else
                Dim Count As Long = ReadData.Count / 2
                For i = 0 To Count - 1
                    Result.Add(ReadData.Item(i))
                    Result.Add(ReadData.Item(i + Count))
                    CurrentValue += 1
                Next
                ReadData = Result
            End If
            Try
                Dim tth As Threading.Thread = Threading.Thread.CurrentThread
                tth.Abort()
            Catch

            End Try
        Catch ex As Exception
            If ex.Message <> "正在中止线程。" Then
                Me.Invoke(New InvokeEnd(AddressOf EndFind), True, ex.Message)
            Else
                Me.Invoke(New InvokeEnd(AddressOf EndFind), False, "")
            End If
            Try
                th.Abort()
            Catch
            End Try
        End Try
    End Sub

    Private Sub 导出数据ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 导出数据ToolStripMenuItem.Click
        Dim Dialog As New SaveFileDialog
        If ReadData.Count <= 0 Then MessageBox.Show("没有找到可保存数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) : Return
        With Dialog
            .Title = "保存"
            .Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
            .CheckPathExists = True
            .FileName = "1"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                If File.Exists(.FileName) Then If MessageBox.Show(Me, "文件已经存在是否覆盖？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then Return
                Dim fs As New FileStream(.FileName, FileMode.Create, FileAccess.Write)
                Dim fw As New StreamWriter(fs, System.Text.Encoding.Default)
                For i = 0 To ReadData.Count - 1
                    For j = 0 To ReadData.Item(i).Data.Count - 1
                        fw.WriteLine(ReadData.Item(i).Data.Item(j).ToString)
                    Next
                Next
                fw.Close()
                fs.Close()
                MessageBox.Show(Me, "数据保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End With
    End Sub
End Class